//
//  LBIMMessageHandle.m
//  app
//
//  Created by 刘文扬 on 16/9/7.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBIMMessageHandle.h"


#import "NSString+LBDate.h"
#import "LBDiscussBidMessage.h"
#import "LBRongYunKitHead.h"
#import <LBPrefix.h>

@implementation LBIMMessageHandle

-(instancetype)init
{
    self = [super init];
    if (self) {
        
        //注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleIMMessage:) name:kNotificationKeyLBIMMessage object:nil];
    }
    return self;
}


-(void)handleIMMessage:(NSNotification *)notification
{
    RCMessage *message = notification.object;
    if (message.conversationType == ConversationType_SYSTEM) {
        return;
    }else if (message.conversationType == ConversationType_PRIVATE){
        //单聊,用于接发任务
        
        BACK(^(){
            RCConversation *conversation = [[RCIMClient sharedRCIMClient] getConversation:ConversationType_PRIVATE targetId:message.targetId];
            MAIN(^(){
                
                LBIMMessageType type = LBIMMessageText;
                RCMessageContent *lastMessage = conversation.lastestMessage;
                long long time;
                
                NSString *imUserId =  [LBRCIMManager shareInstance].rcIMInfo.imUserId;
                
                if ([conversation.senderUserId isEqualToString:imUserId]) {
                    time = conversation.sentTime;
                }else{
                    time = conversation.receivedTime;
                }
                
                NSString *content = nil;
                if ([lastMessage isMemberOfClass:[RCTextMessage class]]) {
                    type = LBIMMessageText;
                    
                    content = ((RCTextMessage *)lastMessage).content;
                    
                }else if([lastMessage isMemberOfClass:[RCImageMessage class]]){
                    type = LBIMMessageImage;
                    content = @"[图片]";
                }else if ([lastMessage isMemberOfClass:[RCVoiceMessage class]]){
                    type = LBIMMessageVoice;
                    content = @"[语音]";
                }
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(LBIMMessageHandle:time:unReadMessageNumber:groupId:messageContent:messageType:)]) {
                    [self.delegate LBIMMessageHandle:self time:time unReadMessageNumber:conversation.unreadMessageCount groupId:conversation.targetId messageContent:content messageType:type];
                }
            });
        });
        
        
    }else if (message.conversationType == ConversationType_GROUP){
        //群聊,用于招投标
        BACK(^(){
            RCConversation *conversation = [[RCIMClient sharedRCIMClient] getConversation:ConversationType_GROUP targetId:message.targetId];
            MAIN(^(){
                
                LBIMMessageType type = LBIMMessageText;
                RCMessageContent *lastMessage = conversation.lastestMessage;
                long long time;
                
                NSString *imUserId =  [LBRCIMManager shareInstance].rcIMInfo.imUserId;
                
                if ([conversation.senderUserId isEqualToString:imUserId]) {
                    time = conversation.sentTime;
                }else{
                    time = conversation.receivedTime;
                }
                
                NSString *content = nil;
                if ([lastMessage isMemberOfClass:[RCTextMessage class]]) {
                    type = LBIMMessageText;
                    
                    content = ((RCTextMessage *)lastMessage).content;
                    
                }else if([lastMessage isMemberOfClass:[RCImageMessage class]]){
                    type = LBIMMessageImage;
                    content = @"[图片]";
                }else if ([lastMessage isMemberOfClass:[RCVoiceMessage class]]){
                    type = LBIMMessageVoice;
                    content = @"[语音]";
                }
                
                if ([message.objectName isEqualToString:LBDiscussBidMessageIdentifier]) {
                    content = @"您现在可以开始议标";
                }
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(LBIMMessageHandle:time:unReadMessageNumber:groupId:messageContent:messageType:)]) {
                    [self.delegate LBIMMessageHandle:self time:time unReadMessageNumber:conversation.unreadMessageCount groupId:conversation.targetId messageContent:content messageType:type];
                }
            });
        });

    }
    
        
    
    
    
    NSLog(@"%@",message);
    
}

+(LBBidDiscussIMShowModel *)getDiscussModelByGroupId:(NSString *)groupId
{
    RCConversation *conversation = [[RCIMClient sharedRCIMClient] getConversation:ConversationType_GROUP targetId:groupId];
    if (conversation == nil) {
        return nil;
    }
    RCMessageContent *lastMessage = conversation.lastestMessage;
    long long time;
    
    NSString *imUserId =  [LBRCIMManager shareInstance].rcIMInfo.imUserId;
    
    if ([conversation.senderUserId isEqualToString:imUserId]) {
        time = conversation.sentTime;
    }else{
        time = conversation.receivedTime;
    }
    
    NSString *content = nil;
    if ([lastMessage isMemberOfClass:[RCTextMessage class]]) {
//        type = LBIMMessageText;
        
        content = ((RCTextMessage *)lastMessage).content;
        
    }else if([lastMessage isMemberOfClass:[RCImageMessage class]]){
//        type = LBIMMessageImage;
        content = @"[图片]";
    }else if ([lastMessage isMemberOfClass:[RCVoiceMessage class]]){
//        type = LBIMMessageVoice;
        content = @"[语音]";
    }
   
    LBBidDiscussIMShowModel *model = [[LBBidDiscussIMShowModel alloc] init];
    model.content = content;
    model.time = time;
    model.unReadMessageCount = (NSInteger)conversation.unreadMessageCount;
    return model;

}

+(LBBidDiscussIMShowModel *)getDiscussModelByTargetId:(NSString *)targetId
{
    RCConversation *conversation = [[RCIMClient sharedRCIMClient] getConversation:ConversationType_PRIVATE targetId:targetId];
    if (conversation == nil) {
        return nil;
    }
    RCMessageContent *lastMessage = conversation.lastestMessage;
    long long time;
    
    NSString *imUserId =  [LBRCIMManager shareInstance].rcIMInfo.imUserId;
    
    if ([conversation.senderUserId isEqualToString:imUserId]) {
        time = conversation.sentTime;
    }else{
        time = conversation.receivedTime;
    }
    
    NSString *content = nil;
    if ([lastMessage isMemberOfClass:[RCTextMessage class]]) {
        //        type = LBIMMessageText;
        
        content = ((RCTextMessage *)lastMessage).content;
        
    }else if([lastMessage isMemberOfClass:[RCImageMessage class]]){
        //        type = LBIMMessageImage;
        content = @"[图片]";
    }else if ([lastMessage isMemberOfClass:[RCVoiceMessage class]]){
        //        type = LBIMMessageVoice;
        content = @"[语音]";
    }
    
    LBBidDiscussIMShowModel *model = [[LBBidDiscussIMShowModel alloc] init];
    model.content = content;
    model.time = time;
    model.unReadMessageCount = (NSInteger)conversation.unreadMessageCount;
    return model;
}

+(RCMessage *)getLasetMessageByGroupID:(NSString *)groupId
{
    RCConversation *conversation = [[RCIMClient sharedRCIMClient] getConversation:ConversationType_GROUP targetId:groupId];
    RCMessage *lastMessage = [[RCIMClient sharedRCIMClient] getMessage:conversation.lastestMessageId];
    return lastMessage;
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
